﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AutoDeploy.Common.Library
{
    /// <summary>
    /// DeploymentObject Can be loaded form a Deployment Object XML XElement
    /// This is an object blueprint that matches the XML configuration file. 
    /// Mark Rowe markwrowe@hotmail.com  http://everythingworkflow.spaces.live.com  ,  Daniel Turnell daniel.turnell@gmail.com
    /// This Project http://autodeploy.codeplex.com
    /// </summary>
    public class DeploymentObject
    {
        /// <summary>
        /// Is the OS 64-bit Windows?
        /// </summary>
        public bool is64Bit { get; set; }
        /// <summary>
        /// Name of the Deployment Object Example: "ESB Toolkit 2.1"
        ///  <DeploymentObject name="ESBToolKitRemote">
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// The default SQL connection string to use if none is specified.
        /// <SQLConnectionString></SQLConnectionString>
        /// </summary>
        public string SQLConnectionString { get; set; }
        /// <summary>
        /// The default username to use if imitate is set and the specific command RunAsUserName is empty.
        ///  <RunAsUser></RunAsUser>
        /// </summary>
        public string RunAsUserName { get; set; }
        /// <summary>
        /// The default password to use if imitate is set and the specific command RunAsPassword is empty.
        /// <RunAsPassword></RunAsPassword>
        /// </summary>
        public string RunAsPassword { get; set; }
        /// <summary>
        /// A Dictionary of all the prepared Apps 
        /// <PreparedApp> 
		///     <Name>BM.EXE</Name>
		///     <File>[Program Files]\Microsoft BizTalk Server 2009\Tracking\BM.exe</File>
		///     <Description>To run the BAM management utility, you must be member of the db_owner SQL Server Database role in the BAM Primary Import, BAM Star Schema, and BAM Archive databases. You must also have db_owner permissions on the BAM Notification databases.</Description>
		///     <OnError>Bam management utility could not be found. (and we checked all local drives possible)</OnError>
	    /// </PreparedApp>
        /// </summary>
        public Dictionary <string, string> PreparedApps { get; set; }
        /// <summary>
        /// 
        /// Example:
        /// <CommandSet priority="3">
        ///<Part>PreReq::true</Part>
        /// <Description>Checking for a legitimate version of SQL Server 2005-2008 R2</Description>
        ///  <Command>isInstalled</Command>
	    ///<Values>
        ///        <Value>Microsoft SQL Server 2008::true</Value>
	    ///	    <Value>Microsoft SQL Server 2005::true</Value>
	    ///	    <Value>Microsoft SQL Server 2008 R2::true</Value>
	    ///</Values>
        ///    <Success>SQL Server Check Passed.</Success>
        ///   <Error>Microsoft SQL Server 2005, 8, or 8 R2 or higher Must be installed Link: http://www.microsoft.com/sqlserver/2008/en/us/try-it.aspx"</Error>
        /// </CommandSet>
        /// </summary>
        public List<CommandSet> CommandSets { get; set; }
        /// <summary>
        /// List that holds recoverable error strings. 
        /// </summary>
        public List<string> Errors { get; set; }
        /// <summary>
        /// FastDeploy true : to automate the running of the commands, you should only set this to true if you completely trust the configuration file and all commands related. 
        /// </summary>
        public bool FastDeploy  { get; set; }
        /// <summary>
        /// Constructor
        /// </summary>
        public DeploymentObject()
        {
            is64Bit = false;
            Name = string.Empty;
            SQLConnectionString = string.Empty;
            RunAsUserName = string.Empty;
            RunAsPassword = string.Empty;
            PreparedApps = new Dictionary<string, string>();
            CommandSets = new List<CommandSet>();
            Errors = new List<string>();
            FastDeploy = false;
        }
        /// <summary>
        /// The properly formatted ToString()
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(string.Format("is64Bit:{0}\n",is64Bit.ToString()));
            sb.Append(string.Format("Name:{0}\n",Name));
            sb.Append(string.Format("SQLConnectionString:{0}\n",SQLConnectionString));
            sb.Append(string.Format("RunAsUserName:{0}\n", RunAsUserName));
            sb.Append(string.Format("RunAsPassword:{0}\n", RunAsPassword));
            foreach (var item in PreparedApps)
            {
                sb.Append(string.Format("PreparedApp Key:{0}\n", item.Key));
                sb.Append(string.Format("PreparedApp Value:{0}\n", item.Value));
            }
            foreach(var commandSet in CommandSets)
            {
                sb.Append(string.Format("CommandSet:{0}\n",commandSet.ToString()));
            }
            sb.Append(string.Format("Errors:{0}\n",StringHelper.ListOfStringToString(Errors,true)));
            return sb.ToString();

        }
    }
}
